import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:t_mb_03/file.dart';
import 'package:t_mb_03/get_http.dart';
import 'package:t_mb_03/menu.dart';
import 'package:t_mb_03/music_player.dart';

class xq_home extends StatefulWidget {
  const xq_home({super.key});

  @override
  State<xq_home> createState() => _xq_homeState();
}

class _xq_homeState extends State<xq_home> {
  ScrollController scrollController = ScrollController();
  double font_size = 24;
  Color bg_color = Colors.white;
  Color font_color = Colors.black;
  late Timer xq_time_timer;
  int xq_time_count = 0;
  double pixels_ = 0;
  double maxScrollExtent_ = 0;
  bool jd_flag = false;

  void xq_start_timer(){
    xq_time_timer = Timer.periodic(Duration(seconds: 1), (timer) {
      xq_time_count += 1;
      setState(() {

      });
    });
  }

  void xq_stop_timer(){
    xq_time_timer.cancel();
  }


  @override
  void initState() {
    // TODO: implement initState
    xq_start_timer();
    scrollController.addListener(() {
      pixels_ = scrollController.position.pixels;
      maxScrollExtent_ = scrollController.position.maxScrollExtent;
      if(pixels_ == maxScrollExtent_){
        jd_flag = true;
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    xq_stop_timer();
    String title = API.xp_dic["title"];
    int word_count = API.xp_dic["word_count"];
    int res = 0;
    if(maxScrollExtent_ > 0){
      res = (word_count / maxScrollExtent_ * pixels_).toInt();
    }
    var dic = read_sj_file();
    if(dic.containsKey(title)){
      var l = dic[title]!;
      int count = l[4];
      count += xq_time_count;
      if(jd_flag){
        save_sj_file(API.xp_dic["id"], title, API.xp_dic["cover_image"], API.xp_dic["author_name"], count, API.xp_dic["word_count"], API.xp_dic["word_count"]);
      }
      else{
        save_sj_file(API.xp_dic["id"], title, API.xp_dic["cover_image"], API.xp_dic["author_name"], count, max(l[5], res), API.xp_dic["word_count"]);
      }
    }
    else{
      save_sj_file(API.xp_dic["id"], title, API.xp_dic["cover_image"], API.xp_dic["author_name"], xq_time_count, res, API.xp_dic["word_count"]);
    }
    xq_time_count = 0;
    time_timer?.cancel();
    time_flag = false;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // print(API.xp_dic);
    return Scaffold(
      body: DefaultTextStyle(
        style: TextStyle(fontSize: 24, color: Colors.black),
        child: Row(
          children: [
            Container(
              width: 350,
              height: MediaQuery.of(context).size.height,
              color: Colors.grey.shade400,
              child: Padding(
                padding: const EdgeInsets.all(12.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Icon(
                          Icons.access_time,
                          color: Colors.white,
                          size: 70,
                        ),
                        Text(
                          "阅读中",
                          style: TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 40),
                        ),
                      ],
                    ),
                    Row(
                      children: [
                        Container(
                          margin: EdgeInsets.all(8),
                          width: 120,
                          height: 160,
                          decoration: BoxDecoration(
                            image: DecorationImage(
                              image: NetworkImage(
                                  "${API.ip}/images/${API.xp_dic["cover_image"]}"),
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.all(8),
                          height: 160,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text("${API.xp_dic["title"]}"),
                              Text("${API.xp_dic["author_name"]}"),
                            ],
                          ),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: 8,
                    ),
                    Row(
                      children: [
                        Text("字号:"),
                        SizedBox(
                          width: 8,
                        ),
                        InkWell(
                          onTap: () {
                            font_size -= 1;
                            setState(() {});
                          },
                          child: Container(
                            width: 44,
                            height: 44,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border.all(color: Colors.grey)),
                            child: Text("-"),
                          ),
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        Text("Aa"),
                        SizedBox(
                          width: 8,
                        ),
                        InkWell(
                          onTap: () {
                            font_size += 1;
                            setState(() {});
                          },
                          child: Container(
                            width: 44,
                            height: 44,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border.all(color: Colors.grey)),
                            child: Text("+"),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: 8,
                    ),
                    Row(
                      children: [
                        Text("颜色:"),
                        SizedBox(
                          width: 8,
                        ),
                        InkWell(
                          onTap: () {
                            bg_color = Colors.white;
                            font_color = Colors.black;
                            setState(() {});
                          },
                          child: Container(
                            width: 44,
                            height: 44,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border.all(color: Colors.grey)),
                            child: Text("文"),
                          ),
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        InkWell(
                          onTap: () {
                            bg_color = Colors.black;
                            font_color = Colors.white;
                            setState(() {});
                          },
                          child: Container(
                            width: 44,
                            height: 44,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Colors.black,
                                border: Border.all(color: Colors.grey)),
                            child: Text(
                              "文",
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        InkWell(
                          onTap: () {
                            bg_color = Colors.green;
                            font_color = Colors.black;
                            setState(() {});
                          },
                          child: Container(
                            width: 44,
                            height: 44,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Colors.green,
                                border: Border.all(color: Colors.grey)),
                            child: Text(
                              "文",
                              style: TextStyle(color: Colors.black),
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: 8,
                    ),
                    if (API.music_url.isNotEmpty)
                      Container(
                        width: 210,
                        height: 100,
                        color: Colors.white,
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text("有声阅读"),
                              Row(
                                children: [
                                  InkWell(
                                    onTap: () async {
                                      await MusicPlayer.lead(API.music_url);
                                      MusicPlayer.play();
                                      setState(() {});
                                    },
                                    child: Container(
                                      width: 80,
                                      height: 44,
                                      alignment: Alignment.center,
                                      decoration: BoxDecoration(
                                          color: Colors.white,
                                          border:
                                              Border.all(color: Colors.grey)),
                                      child: Text("播放"),
                                    ),
                                  ),
                                  SizedBox(
                                    width: 18,
                                  ),
                                  InkWell(
                                    onTap: () {
                                      MusicPlayer.stop();
                                      setState(() {});
                                    },
                                    child: Container(
                                      width: 80,
                                      height: 44,
                                      alignment: Alignment.center,
                                      decoration: BoxDecoration(
                                          color: Colors.white,
                                          border:
                                              Border.all(color: Colors.grey)),
                                      child: Text("暂停"),
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        ),
                      ),
                  ],
                ),
              ),
            ),
            Expanded(
              child: Container(
                decoration: BoxDecoration(
                  color: bg_color,
                ),
                child: Padding(
                  padding: const EdgeInsets.all(24.0),
                  child: ListView(
                    controller: scrollController,
                    children: [
                      Text("${API.xp_dic["content"]}", style: TextStyle(color: font_color, fontSize: font_size),),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
